﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.Utils.Win;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using Luna小店.UC.样品管理.样品下单新.service;
using DevExpress.XtraGrid.Demos.util;
using System.IO;

namespace Luna小店.UC.样品管理.样品下单新
{
    public partial class Form样品文件 : DevExpress.XtraEditors.XtraForm
    {
        private DTO备注项目详情 DTO备注项目 = new DTO备注项目详情();
        private string formId;
        public Form样品文件(String formId)
        {
            InitializeComponent();
            this.formId = formId;
            this.searchLookUpEdit项目.Properties.DataSource = 样品下单Service.get备注项目();
        }

        #region 选择项目
        /// <summary>
        /// 绑定清空事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit项目_Popup(object sender, EventArgs e)
        {
            //得到当前SearchLookUpEdit弹出窗体
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn项目_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        /// <summary>
        /// 点击选择后的行为
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit项目View_Click(object sender, EventArgs e)
        {
            var a = this.searchLookUpEdit项目.Properties.View.GetSelectedRows();
            foreach (int rowHandle in a)
            {
                this.DTO备注项目.sampleItemId = this.searchLookUpEdit项目.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                this.DTO备注项目.sampleItemName = this.searchLookUpEdit项目.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                this.DTO备注项目.unique = Convert.ToBoolean(this.searchLookUpEdit项目.Properties.View.GetRowCellValue(rowHandle, "unique").ToString());
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn项目_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit项目.ToolTip = null;
            searchLookUpEdit项目.EditValue = null;
        }
        /// <summary>
        /// 选完之后的显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit项目_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.DTO备注项目.sampleItemName;
        }
        #endregion

        private void simpleButton保存项目_Click(object sender, EventArgs e)
        {
            this.DTO备注项目.id = new FunctionHelper().Uuid;
            if (String.IsNullOrEmpty(this.DTO备注项目.sampleItemId))
            {
                return;
            }
            //上传文件
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "pdf,doc,docx|*.pdf;*.doc;*.docx;*.jpg;";
            ofd.Title = "请选择上传文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                this.DTO备注项目.memo = Path.GetFileName(ofd.FileName);
                //保存备注项
                样品下单Service.post项目(this.formId, this.DTO备注项目);
                样品下单Service.上传文件(this.DTO备注项目.id, ofd); 
                MessageBox.Show("操作成功");
            }
            this.Close();
        }
    }
}